package effect
{
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.*;
	import flash.net.*;
	
	[SWF(backgroundColor="#000000", width="1280", height="960")]
	/**
	 * 图片墙（圆形） 
	 * @author KimWang
	 * 
	 */
	public class CirlcePictureWall extends Sprite
	{
		private var contain:Sprite=new Sprite();//创建容器
		private var list:Array=new Array();//用于图片管理的数
		private var count:int=0;
		private var imageList:Array=new Array();
		private var centerX:Number=stage.stageWidth/2;
		private var centerY:Number=stage.stageHeight/3;
		private var R:int=560;//b半径
		private var num:int=16;//边数
		private var perangle:Number=(num-2)*180/num;
		private var angle:Number=2*Math.PI/num;
		public function CirlcePictureWall()
		{
			init();
			init_loadImage();
		}		
		private function init():void
		{
			addChild(contain);
			contain.x=centerX;
			contain.y=centerY;
			contain.z=250;			
			root.transform.perspectiveProjection.fieldOfView=80;//初始化透视角度
			addEventListener(Event.ENTER_FRAME,Run);
		}
		
		//加载外部图片
		private function init_loadImage():void
		{
			
			addImages("./image/img/pic (1).jpg","./image/img/pic (2).jpg","./image/img/pic (3).jpg","./image/img/pic (4).jpg",
				"./image/img/pic (5).jpg","./image/img/pic (6).jpg","./image/img/pic (7).jpg","./image/img/pic (8).jpg",
				"./image/img/pic (9).jpg","./image/img/pic (10).jpg","./image/img/pic (11).jpg","./image/img/pic (12).jpg",
				"./image/img/pic (13).jpg","./image/img/pic (14).jpg","./image/img/pic (15).jpg","./image/img/pic (16).jpg",
				"./image/img/pic (17).jpg","./image/img/pic (18).jpg","./image/img/pic (19).jpg","./image/img/pic (20).jpg",
				"./image/img/pic (21).jpg","./image/img/pic (22).jpg","./image/img/pic (23).jpg","./image/img/pic (24).jpg",
				"./image/img/pic (25).jpg","./image/img/pic (26).jpg","./image/img/pic (27).jpg","./image/img/pic (28).jpg",
				"./image/img/pic (29).jpg","./image/img/pic (30).jpg","./image/img/pic (31).jpg","./image/img/pic (32).jpg");		
		}
		
		
		//创建二维阵列分布4x8
		private function createSprite(array:Array):void
		{
			var k:int=0;
			for (var i:int=0; i<2; i++)
			{
				for (var j:int=0; j<num; j++)
				{
					var myphoto:MovieClip=new MovieClip();
					contain.addChild(myphoto);
					myphoto.addChild(array[k]);
					list.push(myphoto);
					myphoto.x=R*Math.cos(angle*j);
					myphoto.z=R*Math.sin(angle*j); 
					
					myphoto.y=i*300+10;
					myphoto.rotationY=j*perangle+90;//偏移
					
					k++;
				}
				
			}
			//contain.rotationX=5;//偏移45度
			sortZ();
		}
		
		//添加外部图片，偷懒的做法 
		private function addImages(...args):void
		{
			count=args.length;
			for (var i:int=0; i<args.length; i++)
			{
				var loader:Loader=new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
				loader.load(new URLRequest(args[i]));
			}
		}
		private function onComplete(event:Event):void
		{
			var bitmap:Bitmap=event.currentTarget.content as Bitmap;
			bitmap.x=-bitmap.width/2;
			bitmap.y=-bitmap.height/2;
			imageList.push(bitmap);
			event.currentTarget.removeEventListener(Event.COMPLETE,onComplete);
			event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onError);
			count--;
			if (count==0)
			{
				createSprite(imageList);
			}
			
		}
		private function onError(event:Event):void
		{
			throw new Error("路径错误");
		}
		//排序		
		private function sortZ():void
		{
			list.sort(depthSort);
			
			for (var i:int=0; i<list.length; i++)
			{
				var myimage:MovieClip=list[i];
				contain.setChildIndex(myimage,i);
				
			}
			
		}
		//摘录make thing move 姐妹篇一个管理方法 
		private function depthSort(objA:DisplayObject,objB:DisplayObject):int
		{
			var posA:Vector3D=objA.transform.matrix3D.position;
			posA=contain.transform.matrix3D.deltaTransformVector(posA);
			
			var posB:Vector3D=objB.transform.matrix3D.position;
			posB=contain.transform.matrix3D.deltaTransformVector(posB);
			return posB.z-posA.z;
		}
		
		private function Run(event:Event):void
		{
			contain.rotationY+=0.1;
			sortZ();			
		}
		
	}
}